<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>编辑角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/plugns/zTree/v3/css/metroStyle/metroStyle.css">
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript"
            src="/plugns/zTree/v3/js/jquery.ztree.core-3.5.js"></script>
    <script type="text/javascript" src="/plugns/zTree/v3/js/jquery.ztree.excheck-3.5.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="/js/tools.js"></script>
    <script type="text/javascript" src="/js/common/update-setting.js"></script>
    <link rel="stylesheet" type="text/css" href="//at.alicdn.com/t/font_1041315_0zq2yro0cfn.css">
    <!--<script type="text/javascript">-->
        <!--$(document).ready(function() {-->
            <!--var flag='false';-->
            <!--if(flag){-->
                <!--$("form").disable();-->
            <!--}-->
        <!--});-->
    <!--</script>-->
    <script type="text/javascript" th:inline="javascript">

        var setting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        var zNodes =[[${menus}]];
        $(document).ready(function () {
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        });

        (function ($) {
            $.fn.disable = function () {
                return $(this).find("*").each(function () {
                    $(this).attr("disabled", "disabled");
                });
            }
        })(jQuery);


    </script>
    <!--<script type="text/javascript">-->
        <!--$(document).ready(function () {-->
            <!--var flag = 'false';-->
            <!--if (flag) {-->
                <!--$("form").disable();-->
            <!--}-->
        <!--});-->
    <!--</script>-->
</head>

<body>
<div class="x-body">
    <form class="layui-form layui-form-pane" style="margin-left: 20px;">
        <div style="width:100%;height:400px;overflow: auto;">
            <div class="layui-form-item">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                    <legend style="font-size:16px;">角色信息</legend>
                </fieldset>
            </div>
            <div class="layui-form-item">
                <label for="roleName" class="layui-form-label">
                    <span class="x-red">*</span>角色名称
                </label>
                <div class="layui-input-inline">
                    <input th:value="${role.id}" type="hidden" name="id">

                    <input type="text" th:value="${role.roleName}" id="roleName" name="roleName"
                           lay-verify="roleName"
                           autocomplete="off" class="layui-input">
                </div>
                <div id="ms" class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span><span id="ums">角色名称必填</span>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label for="remark" class="layui-form-label">
                        <span class="x-red">*</span>角色备注
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" th:value="${role.roleDesc}" id="remark" name="roleDesc" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                    <legend style="font-size:16px;">权限</legend>
                </fieldset>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label for="remark" class="layui-form-label">
                        <span class="x-red">*</span>权限选择
                    </label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="menus">
                        <ul id="treeDemo" class="ztree"></ul>
                    </div>
                </div>
            </div>
            <div style="height: 60px"></div>
        </div>

        <div style="width: 100%;height: 55px;background-color: white;border-top:1px solid #e6e6e6;
  position: fixed;bottom: 1px;margin-left:-20px;">
            <div class="layui-form-item" style=" float: right;margin-right: 30px;margin-top: 8px">
                <button class="layui-btn layui-btn-normal" lay-submit=""lay-filter="addRole" ><i class="iconfont icon-fabu-"></i>确认</button>
                <button class="layui-btn layui-btn-primary" id="close">
                    <i class="iconfont icon-xingzhuanggongnengtubiao-"></i> 取消
                </button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/layui/layui.js"></script>

<script>
    var $;
    layui.config({
        base: '/js/common/' //假设这是你存放拓展模块的根目录
    }).use(['form', 'layer','common'], function () {
        $ = layui.jquery;
        var form = layui.form,
            common = layui.common
            , layer = layui.layer;

        //自定义验证规则
        form.verify({
            roleName: function (value) {
                if (value.trim() == "") {
                    return "角色名不能为空";
                }
            }
        });


        //监听提交
        form.on('submit(addRole)', function (data) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            var jsonArr = zTree.getCheckedNodes(true);
            var menus = [];
            for (var item in jsonArr) {
                menus.push(jsonArr[item].id);
            }
            data.field.menus = menus;
            var userSaveLoading = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            $.post("edit",data.field,function (res) {
                if(res.success){
                    top.layer.close(userSaveLoading);
                    common.cmsLaySucMsg("修改成功！");
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭                        //刷新父页面
                    parent.location.reload();
                }else{
                    top.layer.close(userSaveLoading);
                    common.cmsLayErrorMsg(res.message);
                }
            });
            return false;
        });
        $("#close").on("click", function(){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭                        //刷新父页面
            parent.location.reload();
        } )


    });
</script>
</body>

</html>
